package com.brand.prisd.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.brand.prisd.R;
import com.brand.prisd.sp.MySharedPreferences;
import com.orhanobut.logger.Logger;
import com.tuya.smart.android.user.api.ILoginCallback;
import com.tuya.smart.android.user.bean.User;
import com.tuya.smart.home.sdk.TuyaHomeSdk;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_VerificationCode;
    private Button btn_login;
    private EditText et_phoneNum;
    private EditText et_psw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();


    }

    private void initView() {
        et_phoneNum = findViewById(R.id.et_phoneNum);
        et_psw = findViewById(R.id.et_psw);
        tv_VerificationCode = findViewById(R.id.tv_VerificationCode);
        tv_VerificationCode.setOnClickListener(this);
        btn_login = findViewById(R.id.btn_login);
        btn_login.setOnClickListener(this);


    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_VerificationCode:
                VerificationCodeLogin();
                break;
            case R.id.btn_login:
                login();
                break;
        }
    }

    private void login() {
        String phoneNum = et_phoneNum.getText().toString();
        String psw = et_psw.getText().toString();
        //手机密码登陆
        TuyaHomeSdk.getUserInstance().loginWithPhonePassword("86", phoneNum, psw, new ILoginCallback() {
            @Override
            public void onSuccess(User user) {
                Toast.makeText(getApplicationContext(), "登录成功，用户名：" + TuyaHomeSdk.getUserInstance().getUser().getUsername(), Toast.LENGTH_SHORT).show();
                MySharedPreferences.Build(getApplicationContext()).putBoolean("登录状态", true);
                go2MainActivity();

            }

            @Override
            public void onError(String code, String error) {
                Toast.makeText(getApplicationContext(), "code: " + code + "error:" + error, Toast.LENGTH_SHORT).show();
                MySharedPreferences.Build(getApplicationContext()).putBoolean("登录状态", false);
                Logger.e("code: " + code + "  " + "error: " + error);
            }
        });
    }

    private void VerificationCodeLogin() {
        startActivity(new Intent(this, VerificationCodeLoginActivity.class));
//        finish();
    }

    private void go2MainActivity() {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    @Override
    public void onBackPressed() {
        startActivity(new Intent(this,SelectAccountActivity.class));
        finish();
    }
}
